package com.mmall.controller.portal;

import com.mmall.common.Const;
import com.mmall.common.ResponseCode;
import com.mmall.common.ServerResponse;
import com.mmall.pojo.User;
import com.mmall.service.ICartService;
import com.mmall.vo.CartVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping(value = "/cart/")
public class CartController {


        @Autowired
        private ICartService iCartService;


    /**
     * 用来展示购物车
     * @param session
     * @return
     */
    @RequestMapping(value = "ListCart.do")
    @ResponseBody
    public ServerResponse<CartVo> ListCart(HttpSession session){

        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if (user == null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),ResponseCode.NEED_LOGIN.getDesc() );
        }

        return iCartService.ListCart(user.getId());
    }


    /**
     * 添加购物车
     * @param session
     * @param count
     * @param productId
     * @return
     */
    @RequestMapping(value = "addCart.do")
    @ResponseBody
    public ServerResponse<CartVo> addCart(HttpSession session,Integer count,Integer productId){

        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if (user == null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),ResponseCode.NEED_LOGIN.getDesc() );
        }

        return iCartService.addCart(user.getId(),count,productId);
    }


    /**
     * 更新购物车数量
     * @param session
     * @param count
     * @param productId
     * @return
     */
    @RequestMapping(value = "updateCart.do")
    @ResponseBody
    public ServerResponse<CartVo> updateCart(HttpSession session,Integer count,Integer productId){

        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if (user == null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),ResponseCode.NEED_LOGIN.getDesc() );
        }
        return iCartService.updateCart(user.getId(),count,productId);
    }

    /**
     * 删除购物车, productIds 表可以删除多个购物产品
     * @param session
     * @param count
     * @param productIds
     * @return
     */
    @RequestMapping(value = "delectCart.do")
    @ResponseBody
    public ServerResponse<CartVo> delectCart(HttpSession session,String productIds){

        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if (user == null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),ResponseCode.NEED_LOGIN.getDesc() );
        }
        return iCartService.delectCart(user.getId(),productIds);
    }

    //全选
    @RequestMapping(value = "updateAndcheckedALL.do")
    @ResponseBody
    public ServerResponse<CartVo> updateAndcheckedALL(HttpSession session,Integer  productId){

        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if (user == null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),ResponseCode.NEED_LOGIN.getDesc() );
        }
        return iCartService.updateAndcheckedALL(user.getId(),productId,Const.Cart.CHECKED);
    }

    //全反选
    @RequestMapping(value = "updateAndchecked.do")
    @ResponseBody
    public ServerResponse<CartVo> updateAndchecked(HttpSession session,Integer  productId){

        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if (user == null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),ResponseCode.NEED_LOGIN.getDesc() );
        }
        return iCartService.updateAndchecked(user.getId(),productId,Const.Cart.UN_CHECKED);
    }

    //单选
    @RequestMapping(value = "OneupdateAndcheckedALL.do")
    @ResponseBody
    public ServerResponse<CartVo> OneupdateAndcheckedALL(HttpSession session,Integer  productId){

        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if (user == null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),ResponseCode.NEED_LOGIN.getDesc() );
        }
        return iCartService.OneupdateAndcheckedALL(user.getId(),productId,Const.Cart.CHECKED);
    }
    //单反选
    @RequestMapping(value = "OneupdateAndchecked.do")
    @ResponseBody
    public ServerResponse<CartVo> OneupdateAndchecked(HttpSession session, Integer  productId){

        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if (user == null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),ResponseCode.NEED_LOGIN.getDesc() );
        }
        return iCartService.OneupdateAndchecked(user.getId(),productId,Const.Cart.CHECKED);
    }


    //查询加入购物车的数量
    @RequestMapping(value = "selcetCartAndProduct.do")
    @ResponseBody
    public ServerResponse<Integer> selcetCartAndProduct(HttpSession session){

        User user = (User) session.getAttribute(Const.CURRENT_USER);
        //用户没有登录的时候直接返回一个0
        if (user == null){
            return ServerResponse.createBySuccess(0);
        }
        return iCartService.selcetCartAndProduct(user.getId());
    }

}
